import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:shenling_logistics/pages/my/index.dart';
import 'package:shenling_logistics/pages/task/index.dart';
import 'package:shenling_logistics/utils/Auth.dart';
import 'package:shenling_logistics/utils/EventBus.dart';

class Index extends StatefulWidget {
  const Index({super.key});

  @override
  State<Index> createState() => _IndexState();
}

class _IndexState extends State<Index> {
  // 当前高亮索引
  int _currentIndex = 0;
  // 导航栏数据
  List tabBar = [
    {'icon': 'ic_tab_btn_task.svg', 'label': '任务'},
    {'icon': 'ic_tab_btn_mess_nor.svg', 'label': '消息'},
    {'icon': 'ic_tab_btn_mine_nor.svg', 'label': '我的'},
  ];

  @override
  void initState() {
    super.initState();
    // 初始化身份令牌
    Future.delayed(Duration.zero, () async {
      final auth = Auth();
      // 初始化身份令牌
      await auth.init();
      String token = auth.getToken() ?? '';

      if (token.isEmpty) return Navigator.pushNamed(context, '/login');
      // UserDataInEvent u = UserDataInEvent();
      eventBus.fire(UserDataInEvent());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: [
          TaskIndex(),
          Text('消息页'),
          MineIndex(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.white,
        type: BottomNavigationBarType.fixed,
        // 点击切换tab
        onTap: (value) => setState(() {
          _currentIndex = value;
        }),
        currentIndex: _currentIndex,

        items: tabBar.map((item) {
          return BottomNavigationBarItem(
              icon: SvgPicture.asset(
                item['icon'],
                width: 22,
                height: 22,
              ),
              activeIcon: SvgPicture.asset(
                item['icon'],
                width: 22,
                height: 22,
                colorFilter:
                    ColorFilter.mode(Colors.red[800]!, BlendMode.srcIn),
              ),
              label: item['label']);
        }).toList(),
      ),
    );
  }
}
